package com.chixing.day04_oop.work;

import java.util.Random;

//测试类
public class AccountTest {
    public static void main(String[] args) {
        //1.创建两个 储蓄对象 和 信用对象 分别对方法进行测试
        //2.测试透支业务
        //储蓄对象测试
        SavingsAccount savingsAccount=new SavingsAccount("save1","123",1000);
        //存款(初始金额1000)
        savingsAccount.deposit(1000);
        //取款
        savingsAccount.withdraw(1000);
        System.out.println(savingsAccount);

        CreditAccount creditAccount=new CreditAccount("credit1","123",1000,5000,100);
        //存款(初始金额1000)
        creditAccount.deposit(1500);
        //取款 (透支业务)
        creditAccount.withdraw(5500);
        System.out.println(creditAccount);

        //3.测试银行账户是否创建成功
        Bank bank=new Bank(50);
        Account[] accounts = bank.getAccounts();
        int num=0;
        for (int i = 0; i < accounts.length; i++) {
            num++;
        }
        System.out.println("银行当中的对象数量: "+num);

        /**
         * **拓展题：**
         * （1）创建20个账户对象，要求 储蓄账户：信用账户  = 7：3
         * （2）账户帐号由622262开头，再随机产生7位整数组成,账户帐号不能重复
         */

        //代表数组长度
        int size=0;
        //创建20个账户对象，要求 储蓄账户：信用账户  = 7：3
        for (int i = 0; i < 20; i++) {
            //两个入口 分别创建储蓄账户 以及信用账户
            //储蓄账户
            if(i<14){
                //1.生成银行卡号
                String id = randomId();
                //2.判断是否有重复账号
                boolean flag = repeatOrNot(accounts, size, id);
                if(flag)
                    accounts[size++]=new SavingsAccount(id,"123",100);
                else
                    i--;
            }
            //信用账户
            else {
                //1.生成银行卡号
                String id = randomId();
                //2.判断是否有重复账号
                boolean flag = repeatOrNot(accounts, size, id);
                if(flag)
                    accounts[size++]=new CreditAccount(id,"123",100,5000,100);
                else
                    i--;
            }
        }

        //遍历
        for (int i = 0; i < size; i++) {
            System.out.println(accounts[i]);
        }

    }

    //判断账户账号不重复
    public static boolean repeatOrNot(Account[] accounts,int size,String id){
        for (int i = 0; i < size; i++) {
            if(accounts[i].getAccountId().equals(id))
                return false;
        }
        return true;
    }

    //账户帐号由622262开头，再随机产生7位整数组成,账户帐号不能重复
    private static String randomId() {
        String id="622262";
        Random random = new Random();
        int randNum = random.nextInt(8888889) + 1111111;
        id+=randNum;
        return id;
    }


}
